package com.castle.fortress.admin.orders.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.orders.entity.GoodsOrderEntity;
import com.castle.fortress.admin.orders.dto.GoodsOrderDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.orders.service.GoodsOrderService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单表 api 控制器
 *
 * @author Mgg
 * @since 2023-01-13
 */
@Api(tags="订单表api管理控制器")
@RestController
@RequestMapping("/api/orders/goodsOrder")
public class ApiGoodsOrderController {
    @Autowired
    private GoodsOrderService goodsOrderService;


    /**
     * 订单表的分页展示
     * @param goodsOrderDto 订单表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="订单表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("订单表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<GoodsOrderDto>> pageGoodsOrder(GoodsOrderDto goodsOrderDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsOrderDto> page = new Page(pageIndex, pageSize);

        IPage<GoodsOrderDto> pages = goodsOrderService.pageGoodsOrder(page, goodsOrderDto);
        return RespBody.data(pages);
    }

    /**
     * 订单表保存
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("订单表保存")
    @PostMapping("/save")
    public RespBody<String> saveGoodsOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOrderEntity goodsOrderEntity = ConvertUtil.transformObj(goodsOrderDto,GoodsOrderEntity.class);
        if(goodsOrderService.save(goodsOrderEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单表编辑
     * @param goodsOrderDto 订单表实体类
     * @return
     */
    @CastleLog(operLocation="订单表-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("订单表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateGoodsOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null || goodsOrderDto.getId() == null || goodsOrderDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOrderEntity goodsOrderEntity = ConvertUtil.transformObj(goodsOrderDto,GoodsOrderEntity.class);
        if(goodsOrderService.updateById(goodsOrderEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单表删除
     * @param ids 订单表id集合
     * @return
     */
    @CastleLog(operLocation="订单表-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("订单表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteGoodsOrder(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsOrderService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 订单表详情
     * @param id 订单表id
     * @return
     */
    @CastleLog(operLocation="订单表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("订单表详情")
    @GetMapping("/info")
    public RespBody<GoodsOrderDto> infoGoodsOrder(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOrderEntity goodsOrderEntity = goodsOrderService.getById(id);
            GoodsOrderDto goodsOrderDto = ConvertUtil.transformObj(goodsOrderEntity,GoodsOrderDto.class);
        return RespBody.data(goodsOrderDto);
    }




    /**
     * 订单表保存
     * @param goodsOrderDto 订单表实体类
     *           prodName   订单名称
     *           memberId   订单用户id
     *           payType    支付类型
     *           channel    下单渠道
     *           couponName 使用的优惠券名
     *           couponId   使用的优惠券ID (领取记录的ID)
     *           shopId     商家ID
     *           shopName   商家名称
     *            consignee 收货人姓名
     *            phone 手机号
     *            region_code 地址编码拼接 省-市-区
     *            address 地址拼接 省名-市名-区名-详细地址
     *           goodsList  订单内商品信息
     *                 goodsId      商品id
     *                 goodsName    商品名称
     *                 goodsImg     商品缩略图
     *                 goodsCount   商品数量
     *                 spec         商品规格ids
     *                 specName     规格名称
     * @return
     */
    @CastleLog(operLocation="订单表-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("订单表保存")
    @PostMapping("/saveOrder")
    public RespBody<GoodsOrderDto> saveOrder(@RequestBody GoodsOrderDto goodsOrderDto){
        if(goodsOrderDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        return RespBody.data(goodsOrderService.saveOrder(goodsOrderDto));

    }

}
